package com.cskaoyan.controller.wx;/*-*- coding = utf-8 -*-
@Time : 2022/11/22 20:40
@Author : 林涂兵
@File : WxOrderController.java
@Software: IntelliJ IDEA*/

import com.cskaoyan.bean.bo.wxordercommentbo.OrderCommentBo;
import com.cskaoyan.bean.common.BaseParam;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.OrderListVo;
import com.cskaoyan.bean.vo.wx.OrderDetailVo;
import com.cskaoyan.bean.po.MarketOrderGoods;
import com.cskaoyan.bean.bo.OrderSubmitBo;
import com.cskaoyan.bean.vo.wx.OrderSubmitVo;
import com.cskaoyan.service.wx.WxOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RequestMapping("wx/order")
@RestController
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    // 查询订单信息
    @RequestMapping("list")
    public BaseRespVo orderList(BaseParam baseParam,Integer showType){
        OrderListVo orderListVo = wxOrderService.selectOrderByStatusId(baseParam,showType);
        return BaseRespVo.succeed(orderListVo);
    }

    // 取消订单
    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map map){
        wxOrderService.cancelOrderById((Integer) map.get("orderId"));
        return BaseRespVo.succeed();
    }

    // 查询订单详情信息
    @RequestMapping("detail")
    public BaseRespVo orderList(Integer orderId) {
        OrderDetailVo orderDetailVo = wxOrderService.selectOrderDetailById(orderId);
        return BaseRespVo.succeed(orderDetailVo);
    }

    @RequestMapping("refund")
    public BaseRespVo orderRefund(@RequestBody MarketOrderGoods marketOrderGoods) {
        int code = wxOrderService.orderRefund(marketOrderGoods.getOrderId());
        if (code == 0) {
            return BaseRespVo.invalidParameter("申请退款失败！请重试！");
        }
        return BaseRespVo.succeed();
    }

    @RequestMapping("confirm")
    public BaseRespVo orderConfirm(@RequestBody MarketOrderGoods marketOrderGoods) {
        int code = wxOrderService.orderConfirm(marketOrderGoods.getOrderId());
        if (code == 0) {
            return BaseRespVo.invalidParameter("确认失败！请重试！");
        }
        return BaseRespVo.succeed();
    }

    @RequestMapping("delete")
    public BaseRespVo orderDelete(@RequestBody MarketOrderGoods marketOrderGoods) {
        int code = wxOrderService.orderDelete(marketOrderGoods.getOrderId());
        if (code == 0) {
            return BaseRespVo.invalidParameter("删除失败！请重试！");
        }
        return BaseRespVo.succeed();
    }


    @RequestMapping("goods")
    public BaseRespVo orderGoods(MarketOrderGoods marketOrderGoods) {
        MarketOrderGoods marketOrderGoods1 = wxOrderService.orderGoods(marketOrderGoods.getOrderId(), marketOrderGoods.getGoodsId());
        return BaseRespVo.succeed(marketOrderGoods1);
    }

    @RequestMapping("comment")
    public BaseRespVo orderComment(@RequestBody OrderCommentBo orderCommentBo) {
        int code = wxOrderService.orderComment(orderCommentBo);
        if (code == 0) {
            return BaseRespVo.invalidParameter("评论失败！请重试！");
        }
        return BaseRespVo.succeed();
    }

    // 提交订单
    @RequestMapping("submit")
    public BaseRespVo submitOrder(@RequestBody OrderSubmitBo orderSubmitBo){
        OrderSubmitVo submitVo = wxOrderService.submitOrder(orderSubmitBo);

        return BaseRespVo.succeed(submitVo);
    }

    // 付款
    @RequestMapping("prepay")
    public BaseRespVo prepayOrder(@RequestBody Map map){

        Integer orderId = null;
        try {
            orderId = (Integer) map.get( "orderId");
        }catch (Exception e){
            String s  = (String) map.get("orderId");
            orderId = Integer.parseInt(s);
            e.printStackTrace();
        }


        wxOrderService.updateOrderStatus(orderId);
        return BaseRespVo.succeed();
    }

}
